<template>
	<view class='content'>
		<view class='obj_list'>
			<view class='item' @click='nav_jobType(1)'>
				<view class='title'>意向职位</view>
				<view class='desc'>{{positionChildName?positionChildName:job_name}}</view>
				<image class='icon' src='/static/icon7.png'></image>
			</view>
			<view class='item' @click='nav_jobType(2)'>
				<view class='title'>职位层级</view>
				<view class='desc'>{{job_level}}</view>
				<image class='icon' src='/static/icon7.png'></image>
			</view>
			<view class='item' @click='nav_jobType(3)'>
				<view class='title'>授课方式</view>
				<view class='desc'>{{teach_method}}</view>
				<image class='icon' src='/static/icon7.png'></image>
			</view>
			<view class='item' @click='nav_jobType(4)'>
				<view class='title'>意向城市</view>
				<view class='text'>(最多三个城市)</view>
				<view class='desc'>
					<scroll-view class='scroll' scroll-x="true" scroll-with-animation="true">{{citys}}</scroll-view>
				</view>
				<image class='icon' src='/static/icon7.png'></image>
			</view>
			<view class='item' @click='nav_jobType(5)'>
				<view class='title'>期望薪资</view>
				<view class='desc'>{{wages}}</view>
				<image class='icon' src='/static/icon7.png'></image>
			</view>
			<view class='item' @click='nav_jobType(6)'>
				<view class='title'>工作性质</view>
				<view class='desc'>{{job_nat}}</view>
				<image class='icon' src='/static/icon7.png'></image>
			</view>
			<view class='item' @click='nav_jobType(7)'>
				<view class='title'>求职状态</view>
				<view class='desc'>{{job_statu}}</view>
				<image class='icon' src='/static/icon7.png'></image>
			</view>
			<view class='item' @click='nav_jobType(8)'>
				<view class='title'>到岗时间</view>
				<view class='desc'>{{time_arr}}</view>
				<image class='icon' src='/static/icon7.png'></image>
			</view>
			<view class='item' @click='nav_jobType(9)'>
				<view class='title'>期望福利</view>
				<view class='desc'>
					<scroll-view class='scroll' scroll-x="true" scroll-with-animation="true">{{welfs}}</scroll-view>
				</view>
				<image class='icon' src='/static/icon7.png'></image>
			</view>
		</view>
		<view class='con_btn' @click="confirm_object()">{{change?'修改':'保存'}}</view>
		<pickerPop 
			:showMask="show_mask"
			:showIndex="show_index"
			:title="title"
			:pickers="pickers"
			:value="value"
			@closeMask="close_mask"
			@pickerChange="picker_change"
			@confirmValue="confirm_value"
		></pickerPop>
	</view>
</template>

<script>
	import PickerPop from "../../components/pickerPop.vue"
	import home from "../../api/home.js"
	import util from "../../utils/util.js"
	export default{
		data(){
			return {
				type: '',
				userInfo: '',
				info: '',
				city_info: {},
				change: false,
				screening: false,
				isRange: 1,
				positionId: -1,
				job_name:'',
				positionChildId: -1,
				positionChildName: '',
				show_mask: false,
				check_index: '',
				show_index: '',
				title: '',
				pickers: [],
				value: [],
				job_levels: [],
				job_level: '',
				job_level_id: '',
				level_value: [0],
				teach_methods: [],
				teach_method: '',
				teach_method_id: '',
				method_value: [0],
				citys: '',
				intentionProvinceCode: '',
				intentionProvinceName: '',
				intentionCityCode: '',
				intentionCityName: '',
				intentionRegionCode: '',
				intentionRegionName: '',
				wages_list:[
					["面议","1k","2k","3k","4k","5k","6k","7k","8k","9k","10k","11k","12k","13k","14k","15k","16k","17k","18k","19k"],
					["面议"],
				],
				wages: '',
				salaryMin: '',
				salaryMax: '',
				wages_value:[0,0],
				job_nats: [{name:"全职",id:1},{name:"兼职",id:2}],
				job_nat: '',
				workNature: '',
				nat_value: [0],
				job_status: [],
				job_statu: '',
				job_statu_id: '',
				status_value: [0],
				time_arrs: [],
				time_arr: '',
				time_arr_id: '',
				time_value: [0],
				welfs: '',
				welfareIds: '',
				salaryChoose:[],
				welfareDetail:'',
				switchOpen:true
			}
		},
		onLoad(options){
			this.type=options.type
			this.userInfo=uni.getStorageSync("userinfo");
			// 获取数据
			this.get_job_intention();
		},
		onShow() {
			// console.log("onshow")
		},
		methods:{
			// 意向职位
			nav_jobType(index){
				this.check_index=index;
				switch(index) {
				    case 1:
				        uni.navigateTo({
				        	url: "../../pages/jobType/jobType?range="+this.isRange+"&id="+this.positionId+'&child_id='+this.positionChildId
				        })
				        break;
				    case 2:
						this.show_index=1;
						this.title='职位层级';
						this.pickers=this.job_levels;
						this.value=this.level_value;
						this.show_mask=true;
				        break;
					case 3:
						this.show_index=1;
						this.title='授课方式';
						this.pickers=this.teach_methods;
						this.value=this.method_value;
						this.show_mask=true;
						break;
					case 4:
						uni.navigateTo({
							url: "../../pages/jobCity/jobCity?data="+JSON.stringify(this.city_info)+'&citys='+this.citys
						})
						break;
					case 5:
						this.show_index=2;
						this.title='期望薪资';
						// home.get_salary_list().then(res=>{
						// 	console.log(res);
						// 	const data = res.data.data
						// 	const min = []
						// 	const max = []
						// 	data.forEach(item=>{
						// 		min.push(item.salaryRangeMin)
						// 		max.push(item.salaryRangeMax)
						// 	})
						// 	this.wages_list[1]=max
						// 	this.pickers=this.wages_list
						// 	console.log(this.wages_list[1]);
						// })
						// this.wages_list=
						this.pickers=this.wages_list;
						this.value=this.wages_value;
						this.show_mask=true;
						break;
					case 6:
						this.show_index=1;
						this.title='工作性质';
						this.pickers=this.job_nats;
						this.value=this.nat_value;
						this.show_mask=true;
						break;	
					case 7:
						this.show_index=1;
						this.title='求职状态';
						this.pickers=this.job_status;
						this.value=this.status_value;
						this.show_mask=true;
						break;	
					case 8:
						this.show_index=1;
						this.title='到岗时间';
						this.pickers=this.time_arrs;
						this.value=this.time_value;
						this.show_mask=true;
						break;
					case 9:
						uni.navigateTo({
							url: "../../pages/welfare/welfare?ids="+this.welfareIds+'&status=1'
						})
						// uni.$emit('updateaaaa',{msg:this.text})
						uni.setStorageSync('text', this.welfareDetail);
						break;
				}
			},
			update(e){
				console.log(e);
			},
			// 关闭选择框
			close_mask(){
				this.show_mask=false
			},
			// 选择
			picker_change(res){
				// this.value=res;
				if(this.check_index===5){
					// if(res[0]===0){
					// 	this.wages_list[1]=['面议']
					// }else{
					// 	this.wages_list[1]=['面议','1k','2k','3k','4k','5k','6k','7k','8k','9k','10k','11k','12k','13k','14k','15k','16k','17k','18k','19k','20k']
					// 	let arr=[]
					// 	this.wages_list[1].forEach((item,index)=>{
					// 		if(item==this.wages_list[1][res[0]]){
					// 			arr=this.wages_list[1].slice(index+1,this.wages_list[1].length)
					// 		}
					// 	})
					// 	this.wages_list[1]=arr.slice(0)
					// 	// if(this.wages_list[1].length<=0)return this.wages_list[1]=['21K']
					// }
					if(res[0]===0){
						this.value = [0,0]
						this.wages_list[1] = this.wages_list[0].slice(0,1)
					}else{
						const max = this.wages_list[0].concat(["20k"])
						this.wages_list[1] = max.slice(res[0]+1)
						this.value=res;
					}
				}else{
					this.value=res;
				}
			},
			// 确认
			confirm_value(){
				var index=this.check_index;
				switch(index) {
				    case 2:
						this.level_value=this.value;
						this.job_level=this.job_levels[this.value[0]].name;
						this.job_level_id=this.job_levels[this.value[0]].id;
				        break;
					case 3:
						this.method_value=this.value;
						this.teach_method=this.teach_methods[this.value[0]].name;
						this.teach_method_id=this.teach_methods[this.value[0]].id;
					    break;
					case 5:
						this.wages_value=this.value;
						this.wages = this.wages_list[0][this.value[0]]==="面议"?"面议":`${this.wages_list[0][this.value[0]]}-${this.wages_list[1][this.value[1]]}/月`
						// if(this.wages_list[0][this.value[0]]>=this.wages_list[1][this.value[1]]){
						// 	util.page_show_toast("薪资范围选择有误",1500)
						// 	return false;
						// }else{
							// if(this.wages_list[0][this.value[0]]==='面议'){
							// 	this.wages='面议'
							// }else{
							// 	this.wages=this.wages_list[0][this.value[0]]==0?this.wages_list[1][this.value[1]]+'/月':this.wages_list[0][this.value[0]]+'-'+this.wages_list[1][this.value[1]]+'/月';
							// }
							// this.salaryMin=this.wages_list[0][this.value[0]]*1000;
							// this.salaryMax=this.wages_list[1][this.value[1]]*1000;
						// }
						// home.get_salary_list().then(res=>{
						// 	console.log(data);
						// 	const data = res.data.data
						// 	const min = []
						// 	const max = []
						// 	data.forEach(item=>{
						// 		min.push(item.salaryRangeMin)
						// 		max.push(item.salaryRangeMax)
						// 	})
						// 	this.pickers=this.wages_list
						// 	console.log(this.max);
						// })
					    break;
					case 6:
						this.nat_value=this.value;
						this.job_nat=this.job_nats[this.value[0]].name;
						this.workNature=this.job_nats[this.value[0]].id;
					    break;	
					case 7:
						this.status_value=this.value;
						this.job_statu=this.job_status[this.value[0]].name;
						this.job_statu_id=this.job_status[this.value[0]].id;
					    break;	
					case 8:
						this.time_value=this.value;
						this.time_arr=this.time_arrs[this.value[0]].name;
						this.time_arr_id=this.time_arrs[this.value[0]].id;
					    break;	
				}
				this.close_mask();
			},
			// 保存/修改 求职意向
			confirm_object(){
				if(!this.job_name){
					util.page_show_toast("请选择意向职位",1500)
					return false;
				}
				if(!this.job_level){
					util.page_show_toast("请选择职位层级",1500)
					return false;
				}
				if(!this.teach_method){
					util.page_show_toast("请选择授课方式",1500)
					return false;
				}
				if(!this.citys){
					util.page_show_toast("请选择意向城市",1500)
					return false;
				}
				if(!this.wages){
					util.page_show_toast("请选择期望薪资",1500)
					return false;
				}
				if(!this.job_nat){
					util.page_show_toast("请选择工作性质",1500)
					return false;
				}
				if(!this.time_arr){
					util.page_show_toast("请选择到岗时间",1500)
					return false;
				}
				if(!this.welfs){
					util.page_show_toast("请选择期望福利",1500)
					return false;
				}
				
				
				if(this.change){
					this.update_job_intention();
				}else{
					this.add_job_intention();
				}
			},
			get_all_data(){
				uni.showLoading()
				//
				this.get_hierarchy_list();
				this.get_mode_list();
				this.get_salary_list();
				this.get_wanted_list();
				this.get_entry_list();
				//
				uni.hideLoading()
			},
			// 求职意向
			get_job_intention(){
				var params={
					id: this.userInfo.id
				}
				home.get_job_intention(params).then(res=>{
					var info=res.data.data;
					if(info){
						this.info=res.data.data;
						// console.log(info);
						this.change=true;
						this.isRange=info.isRange;
						this.positionId=info.positionId;
						this.positionChildId=info.positionChildId,
						this.positionChildName=info.positionChildName,
						this.job_name=info.positionName;
						this.job_level=info.hierarchyName;
						this.job_level_id=info.hierarchyId;
						this.teach_method=info.teacherModeName;
						this.teach_method_id=info.teacherModeId;
						var arr=[],city='',
							arr1=info.intentionProvinceName?info.intentionProvinceName.split(","):[],
							arr2=info.intentionCityName?info.intentionCityName.split(","):[],
							arr3=info.intentionRegionName?info.intentionRegionName.split(","):[];
						arr1.forEach((item,index)=>{
							if(arr1[index]==arr2[index]){
								city=arr1[index]
							}else{
								city=arr1[index]+'-'+arr2[index]
							}
							if(arr3[index]!=0&&arr3[index]){
								city=city+'-'+arr3[index]
							}
							arr.push(city)
						})
						this.citys=arr.join(",");
						this.intentionProvinceCode=info.intentionProvinceCode;
						this.intentionProvinceName=info.intentionProvinceName;
						this.intentionCityCode=info.intentionCityCode;
						this.intentionCityName=info.intentionCityName;
						this.intentionRegionCode=info.intentionRegionCode;
						this.intentionRegionName=info.intentionRegionName;
						this.salaryMin=info.salaryMin;
						this.salaryMax=info.salaryMax;
						if(info.salaryMin==0 || info.salaryMax==0){
							this.wages='面议'
						}else{
							this.wages=info.salaryMin==0?info.salaryMax/1000+'k/月':info.salaryMin/1000+'k'+'-'+info.salaryMax/1000+'k/月';
						}
						this.workNature=info.workNature;
						this.job_nat=this.job_nats[Number(info.workNature)-1].name;
						this.job_statu=info.jobWantedName;
						this.job_statu_id=info.jobWantedStatusId;
						this.time_arr=info.startTimeName;
						this.time_arr_id=info.startTimeId;
						this.welfs=info.welfareNames;
						this.welfareIds=info.welfareIds;
						this.welfareDetail=info.welfareDetail
						this.city_info.intentionProvinceCode=info.intentionProvinceCode;
						this.city_info.intentionProvinceName=info.intentionProvinceName;
						this.city_info.intentionCityCode=info.intentionCityCode;
						this.city_info.intentionCityName=info.intentionCityName;
						this.city_info.intentionRegionCode=info.intentionRegionCode;
						this.city_info.intentionRegionName=info.intentionRegionName;
						this.salaryChoose[0]=info.salaryMin/1000
						this.salaryChoose[1]=info.salaryMax/1000
					}
					this.get_all_data();
				})
			},
			// 职位层级列表
			get_hierarchy_list(){
				home.get_hierarchy_list().then(res=>{
					var list=res.data.data;
					this.job_levels=list;
					if(this.job_level_id){
						list.forEach((item,index)=>{
							if(item.id==this.job_level_id){
								this.level_value[0]=index;
							}
						})
					}
				})
			},
			// 授课方式列表
			get_mode_list(){
				home.get_mode_list().then(res=>{
					var list=res.data.data;
					this.teach_methods=list;
					if(this.teach_method_id){
						list.forEach((item,index)=>{
							if(item.id==this.teach_method_id){
								this.method_value[0]=index;
							}
						})
					}
				})
			},
			// 薪资范围列表
			get_salary_list(){
				// home.get_salary_list().then(res=>{
				// 	console.log(res.data.data);
				// 	var list=res.data.data,min=[],max=[];
				// 	list.forEach((item,index)=>{
				// 		min.push(Number(item.salaryRangeMin)>0?Number(item.salaryRangeMin)/1000:0)
				// 		max.push(Number(item.salaryRangeMax)/1000)
				// 		if(this.salaryMin==item.salaryRangeMin){
				// 			this.wages_value[0]=index;
				// 		}
				// 		if(this.salaryMax==item.salaryRangeMax){
				// 			this.wages_value[1]=index;
				// 		}
				// 	})
				// 	this.wages_list[0]=min;
				// 	this.wages_list[1]=max;
				// })
			},
			// 求职状态列表
			get_wanted_list(){
				home.get_wanted_list().then(res=>{
					var list=res.data.data;
					this.job_status=list;
					if(this.job_statu_id){
						list.forEach((item,index)=>{
							if(item.id==this.job_statu_id){
								this.status_value[0]=index;
							}
						})
					}
				})
			},
			// 到岗时间列表
			get_entry_list(){
				home.get_entry_list().then(res=>{
					var list=res.data.data;
					// console.log(res.data.data);
					this.time_arrs=list;
					if(this.time_arr_id){
						list.forEach((item,index)=>{
							if(item.id==this.time_arr_id){
								this.time_value[0]=index;
							}
						})
					}
				})
			},
			// 添加求职意向
			add_job_intention(){
				if(!this.switchOpen){
					return
				}
				if(this.wages_list[0][this.wages_value[0]]==='面议'){
					this.salaryMin='0'
					this.salaryMax='0'
				}else{
					this.salaryMin=this.wages_list[0][this.wages_value[0]].substring(0,this.wages_list[0][this.wages_value[0]].length-1)+'000'
					this.salaryMax=this.wages_list[1][this.wages_value[1]].substring(0,this.wages_list[1][this.wages_value[1]].length-1)+'000'
				}
				var params={
					"isRange": this.isRange,
					"positionId": this.positionId,
					"positionName": this.job_name,
					"positionChildId": this.positionChildId>0?this.positionChildId:'',
					"positionChildName": this.positionChildName,
					"hierarchyId": this.job_level_id,
					"hierarchyName": this.job_level,
					"teacherModeId": this.teach_method_id,
					"teacherModeName": this.teach_method,
					"intentionProvinceCode": this.intentionProvinceCode,
					"intentionProvinceName": this.intentionProvinceName,
					"intentionCityCode": this.intentionCityCode,
					"intentionCityName": this.intentionCityName,
					"intentionRegionCode": this.intentionRegionCode,
					"intentionRegionName": this.intentionRegionName,
					"salaryMin": this.salaryMin,
					"salaryMax": this.salaryMax,
					"workNature": this.workNature,
					"jobWantedStatusId": this.job_statu_id,
					"jobWantedName": this.job_statu,
					"startTimeId": this.time_arr_id,
					"startTimeName": this.time_arr,
					"welfareIds": this.welfareIds,
					"welfareNames": this.welfs,
					"welfareDetail":this.welfareDetail
				}
				home.add_job_intention(params).then(res=>{
					util.page_show_toast("添加成功",1500)
					uni.removeStorageSync('text');
					this.switchOpen=false
					setTimeout(()=>{
						uni.navigateBack({
							delta: 1
						})
					},1500)
					if(this.type==2){
						uni.setStorageSync("change_jobObject",1)
					}
				})
			},
			// 修改求职意向
			update_job_intention(){
				if(!this.switchOpen){
					return
				}
				if(this.wages_list[0][this.wages_value[0]]==='面议'){
					this.salaryMin='0'
					this.salaryMax='0'
				}else{
					this.salaryMin=this.wages_list[0][this.wages_value[0]].substring(0,this.wages_list[0][this.wages_value[0]].length-1)+'000'
					this.salaryMax=this.wages_list[1][this.wages_value[1]].substring(0,this.wages_list[1][this.wages_value[1]].length-1)+'000'
				}
				var params={
					"id": this.info.id,
					"isRange": this.isRange,
					"positionId": this.positionId,
					"positionName": this.job_name,
					"positionChildId": this.positionChildId>0?this.positionChildId:'',
					"positionChildName": this.positionChildName,
					"hierarchyId": this.job_level_id,
					"hierarchyName": this.job_level,
					"teacherModeId": this.teach_method_id,
					"teacherModeName": this.teach_method,
					"intentionProvinceCode": this.intentionProvinceCode,
					"intentionProvinceName": this.intentionProvinceName,
					"intentionCityCode": this.intentionCityCode,
					"intentionCityName": this.intentionCityName,
					"intentionRegionCode": this.intentionRegionCode,
					"intentionRegionName": this.intentionRegionName,
					"salaryMin": this.salaryMin,
					"salaryMax": this.salaryMax,
					"workNature": this.workNature,
					"jobWantedStatusId": this.job_statu_id,
					"jobWantedName": this.job_statu,
					"startTimeId": this.time_arr_id,
					"startTimeName": this.time_arr,
					"welfareIds": this.welfareIds,
					"welfareNames": this.welfs,
					"welfareDetail":this.welfareDetail
				}
				home.update_job_intention(params).then(res=>{
					util.page_show_toast("修改成功",1500)
					uni.removeStorageSync('text');
					this.switchOpen=false
					if(this.screening&&this.type==1){
						this.screening=false;
						var pages = getCurrentPages();
						var prevPage = pages[pages.length - 2];
						prevPage.$vm.positionId=this.positionId;
						prevPage.$vm.positionChildId=this.positionChildId;
						prevPage.$vm.jobObjectCitys=this.intentionCityCode;
						prevPage.$vm.screening=true;
					}
					if(this.screening&&this.type==2){
						uni.setStorageSync("change_jobObject",1)
					}
					setTimeout(()=>{
						uni.navigateBack({
							delta: 1
						})
					},1500)
				})
			},
		},
		components:{
			pickerPop: PickerPop
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		width: 100%;
		.obj_list{
			width: 100%;
			margin-bottom: 92rpx;
			.item{
				padding: 32rpx 0;
				margin: 0 32rpx;
				border-bottom: 2rpx solid #F6F7F9;
				display: flex;
				flex-direction: row;
				align-items: center;
				&:last-child{
					border-bottom: none;
				}
				.title{
					width: 120rpx;
					height: 42rpx;
					font-size: 30rpx;
					font-weight: 600;
					line-height: 42rpx;
					color:rgba(63,74,88,1);
				}
				.text{
					height: 34rpx;
					font-size: 24rpx;
					font-weight:400;
					line-height: 34rpx;
					color:rgba(121,133,148,0.5);
					padding: 0 16rpx;
				}
				.desc{
					flex: 1;
					height: 42rpx;
					margin: 0 24rpx;
					overflow: hidden;
					font-size: 28rpx;
					font-weight: 400;
					line-height: 42rpx;
					color:rgba(121,133,148,1);
					text-align: right;
				}
				.scroll{
					text-align: right;
					height: 54rpx;
					line-height: 40rpx;
					overflow: hidden;
					white-space:nowrap;
				}
				.icon{
					width: 24rpx;
					height: 24rpx;
				}
			}
		}
		.con_btn{
			margin: 0 32rpx;
			height: 104rpx;
			line-height: 104rpx;
			text-align: center;
			background:rgba(91,144,255,1);
			border-radius: 16rpx;
			font-size: 36rpx;
			font-weight: 400;
			color: #fff;
		}
	}
</style>
